package com.chg.mymvc.servlet;

import com.chg.mymvc.entity.User;
import com.chg.mymvc.service.UserService;
import com.chg.mymvc.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateuser")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        UserService us = new UserServiceImpl();
        System.out.println("到了updateuser"+req.getParameter("updateNewId"));
        if(req.getParameter("updateNewId")!=null) {
            //想要修改本行数据 那么获取到点击的这一行的id 执行数据库的update（）
            String username = req.getParameter("updatename");
            String password = req.getParameter("updatepassword");
            int id=Integer.parseInt(req.getParameter("updateNewId"));
//        HttpSession session = req.getSession();
//        int id = (int)session.getAttribute(req.getParameter("namename"));
//        System.out.println(id);
            System.out.println(id+"####"+username+"&&&&&"+password);
            User user1 = new User();
            user1.setName(username);
            user1.setPassword(password);
            user1.setId(id);
            us.updateUser(user1);
            resp.sendRedirect("amanage.html");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
